﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.hhxApplication.Commands.User
{
    public class QueryUserHandeler : IRequestHandler<QueryUserCommand, Result<PageResultDto<UserListDto>>>
    {
        /// <summary>
        /// 用户仓储
        /// </summary>
        private readonly IBaseRepository<Users> usersRepository;
        /// <summary>
        /// 员工状态
        /// </summary>
        private readonly IBaseRepository<Employees> employeesRepository;
        /// <summary>
        /// 部门信息
        /// </summary>
        private readonly IBaseRepository<Epartments> epartmentsRepository;
        /// <summary>
        /// 证件类型仓储
        /// </summary>
        private readonly IBaseRepository<IDCards> idCardsbaseRepository;
        /// <summary>
        /// 学历仓储
        /// </summary>
        private readonly IBaseRepository<Studies> studiesRepository;
        /// <summary>
        /// 单位仓储
        /// </summary>
        private readonly IBaseRepository<Units> unitsRepository;
        /// <summary>
        /// 角色构造函数
        /// </summary>
        private readonly IBaseRepository<Roles> rolesrepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="usersRepository"></param>
        /// <summary>
        public QueryUserHandeler(IBaseRepository<Users> usersRepository, IBaseRepository<Employees> employeesRepository, IBaseRepository<Epartments> epartmentsRepository, IBaseRepository<IDCards> idCardsbaseRepository, IBaseRepository<Studies> studiesRepository, IBaseRepository<Units> unitsRepository, IBaseRepository<Roles> rolesrepository)
        {
            this.usersRepository = usersRepository;
            this.employeesRepository = employeesRepository;
            this.epartmentsRepository = epartmentsRepository;
            this.idCardsbaseRepository = idCardsbaseRepository;
            this.studiesRepository = studiesRepository;
            this.unitsRepository = unitsRepository;
            this.rolesrepository = rolesrepository;
        }
        /// 用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result<PageResultDto<UserListDto>>> Handle(QueryUserCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<UserListDto>>() { Code = (int)CommonErrorCode.Search, Data = new PageResultDto<UserListDto>() };
            //分页
            var page = new PageResultDto<UserListDto>() { TotalCount = 0, PageCount = 0, List = new List<UserListDto>() };
            try
            {
                var list = await (from a in (await usersRepository.GetAll())
                                  join b in (await employeesRepository.GetAll())
                                  on a.EmployeeId equals b.EmployeeId
                                  join c in (await epartmentsRepository.GetAll())
                                  on a.EpartmentId equals c.EpartmentId
                                  join d in (await idCardsbaseRepository.GetAll())
                                  on a.IDCarId equals d.IDCarId
                                  join e in (await studiesRepository.GetAll())
                                  on a.StudyId equals e.StudyId
                                  join f in (await unitsRepository.GetAll())
                                  on a.UnitId equals f.UnitId
                                  join g in (await rolesrepository.GetAll())
                                  on a.RolesId equals g.RolesId
                                  select new UserListDto()
                                  {
                                      UserId = a.UserId,
                                      UserName = a.UserName,
                                      UserPwd = a.UserPwd,
                                      UserSex = a.UserSex,
                                      UserMyName = a.UserMyName,
                                      IDCarId = a.IDCarId,
                                      UserIDCard = a.UserIDCard,
                                      UserPhone = a.UserPhone,
                                      UserCollage = a.UserCollage,
                                      UserElect = a.UserElect,
                                      StudyId = a.StudyId,
                                      UnitId = a.UnitId,
                                      EmployeeId = a.EmployeeId,
                                      EpartmentId = a.EpartmentId,
                                      EntryDate = a.EntryDate,
                                      TransferDate = a.TransferDate,
                                      Position = a.Position,
                                      Post = a.Post,
                                      Personnel = a.Personnel,
                                      RolesId = a.RolesId,
                                      IsUser = a.IsUser,
                                      UnitName = f.UnitName,
                                      StudyName = e.StudyName,
                                      IDCardType = d.IDCardType,
                                      EpartmentName = b.EmployeeName,
                                      EmployeeName = c.EpartmentName
                                  }).ToListAsync();
                //总条数
                page.TotalCount = list.Count();
                page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / request.pageSize);
                page.List = list.OrderBy(d => d.UserId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                result.Data = page;
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "用户信息列表显示成功";
            }
            catch (Exception)
            {
                result.Msg = "用户信息列表显示异常";
            }
            return result;
        }


    }
}
